package admin

import (
	"duoke/internal/dao"
	"duoke/internal/model/entity"
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/sirupsen/logrus"
)

var FastMail = new(fastMailCtl)

type fastMailCtl struct{}

// Query 快递查询
func (ctl *fastMailCtl) Query(c *gin.Context) {
	var req model.SearchFastMail
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	// 使用 gvalid 进行结构体验证
	if err := g.Validator().Data(req).Run(c); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	req.Types = "order"
	order, err := service.Order.GetByOrderNo(c, req.OrderNo, "*")
	if err != nil || order.Id == 0 {
		response.Error(c, "订单号错误", nil)
		return
	}
	res, err := service.FastMail.Query(c, &req)
	logrus.Info("快递返回:", res)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	go func() {
		order.TrackingCompany = req.Com
		order.TrackingNumber = req.Num
		order.TrackingState = gconv.Int(res.State)
		err = service.Order.Save(c, order)
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
	}()
	response.Success(c, "获取成功", res.Data)
}

// Poll 快递订阅
func (ctl *fastMailCtl) Poll(c *gin.Context) {
	var req model.SearchFastMail
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	order, err := service.Order.GetByOrderNo(c, req.OrderNo, "*")
	if err != nil || order.Id == 0 {
		response.Error(c, "该订单号不存在", nil)
		return
	}
	if order.TrackingNumber != "" {
		response.Error(c, "该订单号已经发货,不需要重复发货", nil)
		return
	}
	req.Types = "order"
	_, err = service.FastMail.Poll(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	order.TrackingCompany = req.Com
	order.TrackingNumber = req.Num
	order.TrackingState = 1
	err = service.Order.Save(c, order)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "快递投递成功", nil)
}

// CouponQuery 优惠券快递查询
func (ctl *fastMailCtl) CouponQuery(c *gin.Context) {
	var req model.SearchFastMail
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	// 使用 gvalid 进行结构体验证
	if err := g.Validator().Data(req).Run(c); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	req.Types = "coupon"
	var coupon entity.CouponOrder
	err := dao.CouponOrder.Ctx(c).Where("order_no=?", req.OrderNo).Order("id desc").
		Scan(&coupon)
	if err != nil || coupon.Id == 0 {
		response.Error(c, "订单号错误", nil)
		return
	}
	res, err := service.FastMail.Query(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	go func() {
		coupon.TrackingCompany = req.Com
		coupon.TrackingNumber = req.Num
		coupon.TrackingState = gconv.Int(res.State)
		_, err = dao.CouponOrder.Ctx(c).Save(&coupon)
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
	}()
	response.Success(c, "获取成功", res.Data)
}

// CouponPoll 优惠券快递订阅
func (ctl *fastMailCtl) CouponPoll(c *gin.Context) {
	var req model.SearchFastMail
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	var order entity.CouponOrder
	err := dao.CouponOrder.Ctx(c).Where("order_no=?", req.OrderNo).Scan(&order)
	if err != nil || order.Id == 0 {
		response.Error(c, "该订单号不存在", nil)
		return
	}
	if order.TrackingNumber != "" {
		response.Error(c, "该订单号已经发货,不需要重复发货", nil)
		return
	}
	req.Types = "coupon"
	_, err = service.FastMail.Poll(c, &req)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	order.TrackingCompany = req.Com
	order.TrackingNumber = req.Num
	order.TrackingState = 1
	_, err = dao.CouponOrder.Ctx(c).Save(&order)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "获取成功", nil)
}
